<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
    <script type="text/javascript" src="jquery-3.5.1.min.js"></script>
</head>
<body>

<table border="1">
    <tr>
        <th>商品编号</th>
        <th>商品名称</th>
        <th>商品详细</th>
        <th>数量</th>
        <th>原价</th>
        <th>秒杀价</th>
        <th>开始时间</th>
        <th>结束时间</th>
        <th>操作</th>
    </tr>
    <tr  th:each="seckillProduct : ${seckillProducts}">
        <td th:text="${seckillProduct.seckillProductId}"></td>
        <td th:text="${seckillProduct.name}" style="width: 100px;"></td>
        <td th:text="${seckillProduct.detail}" style="width: 500px;"></td>
        <th th:text="${seckillProduct.number}"></th>
        <th th:text="${seckillProduct.price}+'元'" style="text-decoration: line-through;"></th>
        <th th:text="${seckillProduct.seckillPrice}+'元'"></th>
        <th th:text="${seckillProduct.startTimeStr}"></th>
        <th th:text="${seckillProduct.endTimeStr}"></th>
        <th ><a href="javascript:getSeckillPath(1)">抢 购</a></th>
    </tr>
</table>

<a href="javascript:orderList()">订单列表</a>


</body>
</html>


<script type="text/javascript" th:inline="javascript">

    /*点击抢购之后 就访问后端 获取一个秒杀地址pathId*/
    function getSeckillPath(seckillProductId) {
        $.ajax({
            url :"/iseckill/getPath/[(${userId})]/"+seckllProductId,
            type : "GET",
            success:function(data){
                if (data.code == 1) {//
                    var path = data.data
                    go(path,seckillProductId)
                }else {
                    alert(data.msg);
                }
            },
            error :function () {
                alert("异常");
            }
        })

    }

    function go(path,seckillProductId){
        $.ajax({
            url :"/seckill/go/[(${userId})]/"+seckillProductId+"/"+path,//安全优化，带着这个path去访问
            type : "GET",
            success:function(data){
                if (data.code == 1) {//成功 就跳转 订单页面
                    //若果返回成功,跳转到订单列表
                    var seckillOrderId = data.data
                    order_pay(seckillOrderId);
                }else {
                    alert(data.msg);
                }
            },
            error :function () {
                alert("异常");
            }
        })
    }



  function orderList() {
        window.location.href = "/seckill/order/now/[(${userId})]";
    }
  function order_pay(seckillOrderId) {
        window.location.href = "/order_pay?seckillOrderId="+seckillOrderId +"&userId=[(${userId})]";
    }

</script>